package com.amsp.oa.oademo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.amsp.oa.oademo.entity.FileDistribution;
import com.amsp.oa.oademo.entity.FileDistributionRecord;
import com.amsp.oa.oademo.mapper.FileDistributionMapper;
import com.amsp.oa.oademo.mapper.FileDistributionRecordMapper;
import com.amsp.oa.oademo.service.FileDistributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FileDistributionServiceImpl extends ServiceImpl<FileDistributionMapper, FileDistribution>
        implements FileDistributionService {

    @Autowired
    private FileDistributionRecordMapper recordMapper;

    @Override
    public IPage<FileDistribution> getPage(Integer page, Integer size, String keyword) {
        Page<FileDistribution> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<FileDistribution> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.hasText(keyword)) {
            wrapper.like(FileDistribution::getFileNumber, keyword)
                    .or()
                    .like(FileDistribution::getFileName, keyword);
        }

        wrapper.orderByDesc(FileDistribution::getCreateTime);
        return this.page(pageParam, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateDistribution(FileDistribution fileDistribution) {
        // 保存主表信息
        boolean success = this.saveOrUpdate(fileDistribution);
        if (!success) {
            return false;
        }

        // 如果有明细记录，保存明细
        if (fileDistribution.getRecords() != null && !fileDistribution.getRecords().isEmpty()) {
            // 如果是更新，先删除原有明细
            if (fileDistribution.getId() != null) {
                LambdaQueryWrapper<FileDistributionRecord> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(FileDistributionRecord::getDistributionId, fileDistribution.getId());
                recordMapper.delete(wrapper);
            }

            // 保存新的明细记录
            for (FileDistributionRecord record : fileDistribution.getRecords()) {
                record.setDistributionId(fileDistribution.getId());
                recordMapper.insert(record);
            }
        }

        return true;
    }

    @Override
    public FileDistribution getDetailById(Long id) {
        // 获取主表信息
        FileDistribution fileDistribution = this.getById(id);
        if (fileDistribution != null) {
            // 查询明细记录
            LambdaQueryWrapper<FileDistributionRecord> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FileDistributionRecord::getDistributionId, id);
            List<FileDistributionRecord> records = recordMapper.selectList(wrapper);
            fileDistribution.setRecords(records);
        }
        return fileDistribution;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeWithRecords(Long id) {
        // 先删除明细记录
        recordMapper.delete(new LambdaQueryWrapper<FileDistributionRecord>()
                .eq(FileDistributionRecord::getDistributionId, id));

        // 再删除主表记录
        return this.removeById(id);
    }
}